package com.fintech.third.service.credit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fintech.common.utils.ConvertUtil;
import com.fintech.datagroud.dao.bairong.EdcBrCreditRiskEvaluationMapper;
import com.fintech.datagroud.dao.bairong.IntBrEquipmentCheckMapper;
import com.fintech.datagroud.dao.decision.DecisionDataMapper;
import com.fintech.datagroud.dao.moxie.IntMxAccountBaseInfoMapper;
import com.fintech.datagroud.dao.moxie.IntMxAlipayZmsCoreMapper;
import com.fintech.datagroud.dao.moxie.IntMxCallDetailsMapper;
import com.fintech.datagroud.dao.moxie.IntMxCallThirdPartyDetailMapper;
import com.fintech.datagroud.dao.moxie.IntMxUserBasicInfoMapper;
import com.fintech.datagroud.dao.moxie.v2.IntMxv2PersistenceMapper;
import com.fintech.datagroud.entity.bairong.IntBrEquipmentCheck;
import com.fintech.third.dao.AcLinkmanMobileMapper;
import com.fintech.third.dao.EdcDeviceInfoMapper;
import com.fintech.third.dao.moxie.IntMxBehaviorCheckMapper;
import com.fintech.third.dao.predictmessage.EdcMessagePredictInfoMapper;
import com.fintech.third.dao.youdun.EdcYdIdDetailMapper;
import com.fintech.third.model.ThirdProperties;
import com.fintech.third.model.credit.BRDeviceInfo;
import com.fintech.third.model.credit.CallCountFilterDto;
import com.fintech.third.model.credit.CreditReportMessageDto;
import com.fintech.third.model.credit.CreditReportPortrait;
import com.fintech.third.model.credit.EdcDeviceDto;
import com.fintech.third.model.credit.MxAlipayAddressDto;
import com.fintech.third.model.credit.MxAlipayBankDto;
import com.fintech.third.model.credit.MxAlipayUserInfoDto;
import com.fintech.third.model.credit.MxOperatorReportDto;
import com.fintech.third.model.credit.PieChartDto;
import com.fintech.third.model.credit.YDDeviceLinkDto;
import com.fintech.third.model.credit.YDFeatureDto;
import com.fintech.third.model.credit.YDLendBehaviorDto;
import com.fintech.third.model.credit.YDUserInfoDto;
import com.fintech.third.model.moxie.MoXieTaskType;
import com.fintech.third.model.moxie.carries.MXUserReportCall;
import com.fintech.third.model.moxie.carriesv2.MXBehaviorCheck;
import com.fintech.third.model.moxie.carriesv2.MXCheckSearchInfo;
import com.fintech.third.model.moxie.dataprovide.IntMxAccountBaseInfo;
import com.fintech.third.model.moxie.dataprovide.IntMxAlipayZmsCore;
import com.fintech.third.model.moxie.dataprovide.IntMxCallThirdPartyDetail;
import com.fintech.third.model.moxie.dataprovide.IntMxUserBasicInfo;
import com.fintech.third.model.predictmessage.EdcMessagePredictInfo;
import com.fintech.third.model.shzx.EdcZxPersonalNewCredit;
import com.fintech.third.model.youdun.EdcYdIdDetail;
import com.fintech.third.service.moxie.MoXieService;
import com.fintech.third.utils.DateUtil;
import com.fintech.third.utils.ThirdPropertiesInitUtil;
import com.fintech.user.entity.User;
import com.fintech.user.service.UserService;
import com.marvels.approve.common.service.ApproveSharedDataService;
import com.marvels.common.utils.MarvelsLogUtil;

import cn.hutool.core.util.StrUtil;

@Service("creditReportService")
public class CreditReportServiceImpl implements CreditReportService {
	
	@Autowired
	private IntMxAccountBaseInfoMapper intMxAccountBaseInfoMapper;
	@Autowired
	private IntMxCallDetailsMapper intMxCallDetailsMapper;
	
	@Autowired
	private IntMxCallThirdPartyDetailMapper intMxCallThirdPartyDetailMapper;
	
	@Autowired
	private IntMxv2PersistenceMapper intMxv2PersistenceMapper;
	
	@Autowired
	private IntMxAlipayZmsCoreMapper intMxAlipayZmsCoreMapper;
	@Autowired
	IntMxUserBasicInfoMapper intMxUserBasicInfoMapper;
	
	@Autowired
	EdcDeviceInfoMapper edcDeviceInfoMapper;
	
	@Autowired
	private AcLinkmanMobileMapper acLinkmanMobileMapper;
	
	@Autowired
	private IntBrEquipmentCheckMapper intBrEquipmentCheckMapper;
	
	@Autowired
	private IntMxBehaviorCheckMapper intMxBehaviorCheckMapper;
	
	@Autowired
	private EdcYdIdDetailMapper edcYdIdDetailMapper;

	@Autowired
	private MoXieService freshMoXie;
	
	@Autowired
	private EdcMessagePredictInfoMapper edcMessagePredictInfoMapper;
	
	@Autowired
	private DecisionDataMapper decisionDataMapper;
	
	@Autowired
	private ApproveSharedDataService approveSharedDataService;
	
	@Autowired
	private UserService userService;
	@Override
	public IntMxAccountBaseInfo getUserOperatorSummary(String loanId) {
		 List<IntMxAccountBaseInfo> mxAccountBaseInfoList = intMxAccountBaseInfoMapper.selectByLoanId(loanId);
		 if(CollectionUtils.isNotEmpty(mxAccountBaseInfoList)) {
			 IntMxAccountBaseInfo  info = mxAccountBaseInfoList.get(0);
			 if(StringUtils.isNotBlank(info.getOpenTime()))
				 info.setInTime(DateUtil.dayComparePrecise(info.getOpenTime(), "yyyy-MM-dd"));
			 if(StringUtils.isNotBlank(info.getCarrier())) {
				 if("CHINA_MOBILE".equals(info.getCarrier()))
					 info.setCarrier("中国移动");
				 else if("CHINA_UNICOM".equals(info.getCarrier()))
					 info.setCarrier("中国联通");
				 else if("CHINA_TELECOM".equals(info.getCarrier()))
					 info.setCarrier("中国电信");
			 }
			 return info;
		 }
		 else
			 return null;
	}

	@Override
	public List<PieChartDto> selectCallLocationPercent(String loanId) {
		return intMxCallDetailsMapper.selectCallLocationPercent(loanId);
	}

	@Override
	public List<PieChartDto> selectCallTypePercent(String loanId) {
		return intMxCallDetailsMapper.selectCallTypePercent(loanId);
	}
	
	
	/**
	 * 
	* @Title: getAutoDecisionData 
	* @Description:拉取自动审核数据  
	* @param loanId
	* @param userId
	* @return       
	* @author 李瑶
	* @date 2018年10月13日 上午10:54:59
	 */
	@Override
	public Map<String, Object> getAutoDecisionData(String loanId, String orgLoanId, String userId, String mobileNo){
		Map<String, Object> map = new HashMap<>();
		map.put("loanId", orgLoanId);
		IntMxAlipayZmsCore intMxAlipayZmsCore = intMxAlipayZmsCoreMapper.selectByLoanId(loanId);
		if(intMxAlipayZmsCore!=null){
			int score = intMxAlipayZmsCore.getZmScore();
			map.put("antScore", score);
		}
		
		//获取短信模型分数
		EdcMessagePredictInfo edcMessagePredictInfo = edcMessagePredictInfoMapper.selectByLoanId(orgLoanId);
		if(edcMessagePredictInfo != null){
			int msgScore = edcMessagePredictInfo.getScore();
			map.put("msgScore", msgScore);
		}
		
		MXBehaviorCheck mXBehaviorCheck = intMxBehaviorCheckMapper.selectByLoanIdAndPoint(loanId, "phone_power_off");
		if(mXBehaviorCheck != null){
			String closeDay = mXBehaviorCheck.getResult();
			if(StringUtils.isNotEmpty(closeDay)){
				String day = closeDay.replaceAll("[^0-9]+(\\d+)[^0-9]+", "$1");
				if(StringUtils.isNotEmpty(day)){
					map.put("closePhoneDays", Integer.valueOf(day));
				}
			}
		}
		
		
		EdcYdIdDetail edcYdIdDetail = edcYdIdDetailMapper.selectByLoanId(loanId);
		if(edcYdIdDetail!=null){
			//百融任务画像分数
			String portrait = edcYdIdDetail.getScore();
			if(org.apache.commons.lang.StringUtils.isNotEmpty(portrait)){
				map.put("portrait", Double.valueOf(portrait));
			}
			
		}
		
		List<IntMxAccountBaseInfo> mxAccountBaseInfoList = intMxAccountBaseInfoMapper.selectByLoanId(loanId);
		if(CollectionUtils.isNotEmpty(mxAccountBaseInfoList)){
			//客户运营商姓名
			IntMxAccountBaseInfo intMxAccountBaseInfo =  mxAccountBaseInfoList.get(0);
			map.put("mxCustName", intMxAccountBaseInfo.getName());
			String inTime = intMxAccountBaseInfo.getInTime();
			if(!"运营商未提供入网时间".equals(inTime)&& StringUtils.isNotEmpty(inTime)){
				int inTimtInt = Integer.valueOf(inTime);
				map.put("in_time", inTimtInt);
			}
			
		}
		
		IntMxCallThirdPartyDetail intMxCallThirdPartyDetail = intMxCallThirdPartyDetailMapper.selectByLoanId(loanId);
		if(intMxCallThirdPartyDetail!=null){
			//最近三个月与催收公司通话按次数
			String collectionTimes = intMxCallThirdPartyDetail.getCallCollectionFirmNum3m();
			if(StringUtils.isNotEmpty(collectionTimes)){
				int collectionTimesInt = Integer.valueOf(collectionTimes);
				map.put("collectionTimes", collectionTimesInt);
			}
			
		}
		
		int contactNum = acLinkmanMobileMapper.selectCountByUserId(userId);
		map.put("contactNum", contactNum);
		Map<String, Object> black = intMxv2PersistenceMapper.selectCheckBlackByLoanId(loanId);
		if(black!=null){
			//直接联系人中引起间接黑名单占比
			map.put("contactForbidenPercent", black.get("directCauseIndirectRate"));
		}
		
		// 获取其他决策数据（上海资信、有盾、魔蝎）
		Map<String, Object> decisionData = decisionDataMapper.selectDecisionDataMapper(loanId);
		if(decisionData != null){
			map.put("actualLoanPlatformCount", isEmpty(decisionData.get("actualLoanPlatformCount")) ? null : Integer.valueOf(decisionData.get("actualLoanPlatformCount").toString()));
			map.put("actualLoanPlatformCount1m", isEmpty(decisionData.get("actualLoanPlatformCount1m")) ? null : Integer.valueOf(decisionData.get("actualLoanPlatformCount1m").toString()));
			map.put("actualLoanPlatformCount3m", isEmpty(decisionData.get("actualLoanPlatformCount3m")) ? null : Integer.valueOf(decisionData.get("actualLoanPlatformCount3m").toString()));
			map.put("actualLoanPlatformCount6m", isEmpty(decisionData.get("actualLoanPlatformCount6m")) ? null : Integer.valueOf(decisionData.get("actualLoanPlatformCount6m").toString()));
			map.put("repaymentPlatformCount", isEmpty(decisionData.get("repaymentPlatformCount")) ? null : Integer.valueOf(decisionData.get("repaymentPlatformCount").toString()));
			map.put("repaymentPlatformCount1m", isEmpty(decisionData.get("repaymentPlatformCount1m")) ? null : Integer.valueOf(decisionData.get("repaymentPlatformCount1m").toString()));
			map.put("repaymentPlatformCount3m", isEmpty(decisionData.get("repaymentPlatformCount3m")) ? null : Integer.valueOf(decisionData.get("repaymentPlatformCount3m").toString()));
			map.put("repaymentPlatformCount6m", isEmpty(decisionData.get("repaymentPlatformCount6m")) ? null : Integer.valueOf(decisionData.get("repaymentPlatformCount6m").toString()));
			map.put("repaymentTimesCount", isEmpty(decisionData.get("repaymentTimesCount")) ? null : Integer.valueOf(decisionData.get("repaymentTimesCount").toString()));
			map.put("loanPlatformCount1m", isEmpty(decisionData.get("loanPlatformCount1m")) ? null : Integer.valueOf(decisionData.get("loanPlatformCount1m").toString()));
			map.put("loanPlatformCount3m", isEmpty(decisionData.get("loanPlatformCount3m")) ? null : Integer.valueOf(decisionData.get("loanPlatformCount3m").toString()));
			map.put("loanPlatformCount6m", isEmpty(decisionData.get("loanPlatformCount6m")) ? null : Integer.valueOf(decisionData.get("loanPlatformCount6m").toString()));
			map.put("loanPlatformCount", isEmpty(decisionData.get("loanPlatformCount")) ? null : Integer.valueOf(decisionData.get("loanPlatformCount").toString()));
			map.put("agencyScore", decisionData.get("agencyScore"));
			map.put("directLinkmanBlackNum", decisionData.get("directLinkmanBlackNum"));
			map.put("indirectLinkmanBlackNum", decisionData.get("indirectLinkmanBlackNum"));
			map.put("causeDirectLinkmanNum", decisionData.get("causeDirectLinkmanNum"));
			map.put("directLinkmanNum", decisionData.get("directLinkmanNum"));
			map.put("directCauseIndirectRate", decisionData.get("directCauseIndirectRate"));
			map.put("searchedOrgCnt", decisionData.get("searchedOrgCnt"));
			map.put("registerOrgCnt", decisionData.get("registerOrgCnt"));
			map.put("loanOverdueTotal", isEmpty(decisionData.get("loanOverdueTotal")) ? null : Integer.valueOf(decisionData.get("loanOverdueTotal").toString()));
			map.put("b24OverdueMonths", isEmpty(decisionData.get("b24OverdueMonths")) ? null : Integer.valueOf(decisionData.get("b24OverdueMonths").toString()));
			map.put("idcardWithOtherNames", decisionData.get("idcardWithOtherNames"));
			map.put("idcardWithOtherPhones", decisionData.get("idcardWithOtherPhones"));
			map.put("phoneWithOtherIdcards", decisionData.get("phoneWithOtherIdcards"));
			map.put("phoneWithOtherNames", decisionData.get("phoneWithOtherNames"));
		}
		
		// 获取特定有盾决策相关数据
		String reqNo = intMxCallDetailsMapper.selectYDReqNo(orgLoanId);
		if(StrUtil.isNotEmpty(reqNo)){
			Map<String, Object> yDDecisionData= decisionDataMapper.selectYDDecisionDataMapper(reqNo);
			if(yDDecisionData != null){
				map.put("mobileCount", isEmpty(yDDecisionData.get("mobileCount")) ? null : Integer.valueOf(yDDecisionData.get("mobileCount").toString()));
				map.put("bankcardCount", isEmpty(yDDecisionData.get("bankcardCount")) ? null : Integer.valueOf(yDDecisionData.get("bankcardCount").toString()));
				map.put("linkUserCount", isEmpty(yDDecisionData.get("linkUserCount")) ? null : Integer.valueOf(yDDecisionData.get("linkUserCount").toString()));
				map.put("userFeatureType",yDDecisionData.get("userFeatureType"));
				map.put("riskEvaluation",yDDecisionData.get("riskEvaluation"));
				map.put("characterPortraitRisk", isEmpty(yDDecisionData.get("characterPortraitRisk")) ? null : Integer.valueOf(yDDecisionData.get("characterPortraitRisk").toString()));
				map.put("otherLinkDeviceCount", isEmpty(yDDecisionData.get("otherLinkDeviceCount")) ? null : Integer.valueOf(yDDecisionData.get("otherLinkDeviceCount").toString()));
				map.put("userHaveBankcardCount", isEmpty(yDDecisionData.get("userHaveBankcardCount")) ? null : Integer.valueOf(yDDecisionData.get("userHaveBankcardCount").toString()));
				map.put("partnerUserCount", isEmpty(yDDecisionData.get("partnerUserCount")) ? null : Integer.valueOf(yDDecisionData.get("partnerUserCount").toString()));
				map.put("linkDeviceCount", isEmpty(yDDecisionData.get("linkDeviceCount")) ? null : Integer.valueOf(yDDecisionData.get("linkDeviceCount").toString()));
			}
		}
		
		// 获取共享平台数据
		Map<String, Object> requestMap=new HashMap<String, Object>();
		requestMap.put("mobileNo", mobileNo);
		Map<String, Object> returnMap = approveSharedDataService.sharedDataQuery(requestMap);
		if(returnMap != null){
			map.put("registerPlatCount", returnMap.get("registerPlatCount"));
			map.put("borrowPlatCount", returnMap.get("borrowPlatCount"));
			map.put("blackPlatCount", returnMap.get("blackPlatCount"));
			map.put("overduePlatCount", returnMap.get("overduePlatCount"));
		}
		
		// 获取魔蝎通话次数相关数据
		Map<String, Object> mXCallNumberData = decisionDataMapper.selectMXCallNumberDataMapper(loanId);
		if(mXCallNumberData != null){
			map.put("call6mTop3Number", isEmpty(mXCallNumberData.get("call6mTop3Number")) ? null : Integer.parseInt(mXCallNumberData.get("call6mTop3Number").toString()));
			map.put("callTenthNumber", isEmpty(mXCallNumberData.get("callTenthNumber")) ? null : Integer.parseInt(mXCallNumberData.get("callTenthNumber").toString()));
			map.put("call3mTop5Number", isEmpty(mXCallNumberData.get("call3mTop5Number")) ? null : Integer.parseInt(mXCallNumberData.get("call3mTop5Number").toString()));
		}
		
		
		// 获取通讯录备注为空的联系人个数
		Integer linkmansRemarkEmptyNumber = decisionDataMapper.selectLinkMansRemarkEmptyNumber(userId);
		if(linkmansRemarkEmptyNumber != null){
			map.put("linkmansRemarkEmptyNumber", linkmansRemarkEmptyNumber);
		}
		
		// 获取有盾借贷行为数据
		Map<String, Object> loanBehaviorData = decisionDataMapper.selectLoanBehaviorData(orgLoanId);
		if(loanBehaviorData != null){
			map.put("loanRepaymentDifference", isEmpty(loanBehaviorData.get("loanRepaymentDifference")) ? null : Double.parseDouble(loanBehaviorData.get("loanRepaymentDifference").toString()));
			map.put("loanApplyProportion", isEmpty(loanBehaviorData.get("loanApplyProportion")) ? null : Double.parseDouble(loanBehaviorData.get("loanApplyProportion").toString()));
		}
		
		// 短信记录出现逾期字样的次数
		Map<String, Object> messageCount = decisionDataMapper.selectMessageCount(orgLoanId);
		if(messageCount != null){
			map.put("overdueMessageCount", isEmpty(messageCount.get("overdueMessageCount")) ? null : Integer.valueOf(messageCount.get("overdueMessageCount").toString()));
			map.put("msgCount", isEmpty(messageCount.get("msgCount")) ? null : Integer.valueOf(messageCount.get("msgCount").toString()));
		}
		
		// 魔蝎相关
		String mxReqNo = intMxCallDetailsMapper.selectMXReqNo(orgLoanId);
		if(StrUtil.isNotEmpty(reqNo)){
			// 近七天被叫且联系人不在通讯录里的次数
			Integer d7CalledNolinkCount= decisionDataMapper.select7dCalledNolinkCount(loanId, mxReqNo, userId);
			if(d7CalledNolinkCount != null){
				map.put("d7CalledNolinkCount", d7CalledNolinkCount);
			}
			// 12599（语音信箱）被叫出现次数
			Integer called12599Count= decisionDataMapper.select12599CalledCount(loanId, mxReqNo);
			if(called12599Count != null){
				map.put("called12599Count", called12599Count);
			}
		}
		
		// 设备信息
		Map<String, Object> deviceInfo = decisionDataMapper.selectDeviceInfo(loanId);
		if(deviceInfo != null){
			map.put("prohibitAppName", deviceInfo.get("apps"));
			map.put("root", deviceInfo.get("root"));
		}
		
		//有盾其他信息
		Map<String, Object> otherLinkDevice = decisionDataMapper.selectOtherLinkDeviceDetail(orgLoanId);
		if(otherLinkDevice != null){
			map.put("otherFrandDeviceCount", otherLinkDevice.get("otherFrandDeviceCount"));
			map.put("otherLivingAttackDeviceCount", otherLinkDevice.get("otherLivingAttackDeviceCount"));
		}
		Map<String, Object> linkDevice = decisionDataMapper.selectLinkDeviceDetail(orgLoanId);
		if(linkDevice != null){
			map.put("frandDeviceCount", linkDevice.get("frandDeviceCount"));
			map.put("livingAttackDeviceCount", linkDevice.get("livingAttackDeviceCount"));
		}
		//归属地个数
		String locationNum = decisionDataMapper.selectlocationNum(loanId);
		if(StrUtil.isNotBlank(locationNum)){
			map.put("locationNum", locationNum);
		}
		
		// useragent
		if(StrUtil.isNotBlank(userId)){
			User user = userService.queryUserById(Integer.parseInt(userId));
			if(user != null){
				map.put("userAgent", user.getUserAgent());
			}
		}
		return map;
	}
	
	
	/**
	 * 
	* @Title: getRiskData 
	* @Description:获取风险数据  
	* @param loanId
	* @param userId
	* @return       
	* @author 李瑶
	* @date 2018年10月22日 上午10:47:34
	 */
	@Override
	public Map<String, Object> getRiskData(String currentLoanId,String hisLoanId,String userId){
		Map<String, Object> map = new HashMap<>();
		IntBrEquipmentCheck intBrEquipmentCheck = intBrEquipmentCheckMapper.selectByLoanId(currentLoanId);
		if(intBrEquipmentCheck != null){
			//金融行业中，匹配设备关联身份证号个数
			String eqcIdNumFinStr =  intBrEquipmentCheck.getEqcIdNumFin();
			if(StringUtils.isNotEmpty(eqcIdNumFinStr)){
				map.put("eqcIdNumFin", Integer.valueOf(eqcIdNumFinStr));
			}else{
				map.put("eqcIdNumFin", "未知");
			}
			//金融行业中，匹配设备关联手机号个数
			String eqcCellNumFinStr = intBrEquipmentCheck.getEqcCellNumFin(); 
			if(StringUtils.isNotEmpty(eqcCellNumFinStr)){
				map.put("eqcCellNumFin", Integer.valueOf(eqcCellNumFinStr));
			}else{
				map.put("eqcCellNumFin", "未知");
			}
			//金融行业中，匹配设备关联身份证号对应关联的设备数量
			String eqcIdGidNumFinStr = intBrEquipmentCheck.getEqcIdGidNumFin(); 
			if(StringUtils.isNotEmpty(eqcIdGidNumFinStr)){
				map.put("eqcIdGidNumFin", Integer.valueOf(eqcCellNumFinStr));
			}else{
				map.put("eqcIdGidNumFin", "未知");
			}
			//金融行业中，匹配设备关联手机号对应关联的设备数量
			String eqcCellGidNumFinStr = intBrEquipmentCheck.getEqcCellGidNumFin(); 
			if(StringUtils.isNotEmpty(eqcCellGidNumFinStr)){
				map.put("eqcCellGidNumFin", Integer.valueOf(eqcCellGidNumFinStr));
			}else{
				map.put("eqcCellGidNumFin", "未知");
			}
		}
		
				
		EdcDeviceDto edcDeviceDto = edcDeviceInfoMapper.selectByLoanId(hisLoanId);
		if(edcDeviceDto != null){
			//安装的贷款app个数
			Integer appCt = edcDeviceDto.getAppCt();
			if(appCt != null){
				map.put("appCt", appCt.intValue());
			}else{
				map.put("appCt", "未知");
			}
			String root = edcDeviceDto.getRoot();
			if(StringUtils.isNotEmpty(root)){
				map.put("root", root);
			}else{
				map.put("root", "未知");
			}
			Integer maliceNum = edcDeviceDto.getSpite_app_cn();
			if(maliceNum != null){
				map.put("maliceNum", maliceNum.intValue());
			}else{
				map.put("maliceNum", "未知");
			}
			String isRunMalice = edcDeviceDto.getRun_spite_app();
			if(StringUtils.isNotEmpty(isRunMalice)){
				map.put("isRunMalice", isRunMalice);
			}else{
				map.put("isRunMalice", "未知");
			}
		}
		return map;
	}
	
	public CallCountFilterDto getCallCount(String loanId) {
		//1.获取主叫次数
		CallCountFilterDto callCountFilterDto = new CallCountFilterDto();
		Map<String,String> param = new HashMap<>();
		param.put("loanId", loanId);
		param.put("type", "DIAL");
		List<PieChartDto>  dialCountList = intMxCallDetailsMapper.getCallCountChart(param);
		
		//2.获取被叫次数
		param.put("type", "DIALED");
		List<PieChartDto> dialedCountList = intMxCallDetailsMapper.getCallCountChart(param);
		
		//3.处理主叫和被叫月份缺失的情况，取两个的合集，对于没有的则补0次
		Map<String,String> months = new HashMap<>();
		Map<String,PieChartDto> dialMonths = new HashMap<>();
		Map<String,PieChartDto> dialedMonths = new HashMap<>();
		if(CollectionUtils.isNotEmpty(dialCountList)) {
			for(PieChartDto pieChartDto : dialCountList) {
				if(StringUtils.isBlank(pieChartDto.getName()))
					continue;
				
				months.put(pieChartDto.getName(), pieChartDto.getName());
				dialMonths.put(pieChartDto.getName(), pieChartDto);
			}
		}
		if(CollectionUtils.isNotEmpty(dialedCountList)) {
			for(PieChartDto pieChartDto : dialedCountList) {
				if(StringUtils.isBlank(pieChartDto.getName()))
					continue;
				
				months.put(pieChartDto.getName(), pieChartDto.getName());
				dialedMonths.put(pieChartDto.getName(), pieChartDto);
			}
		}
		
		callCountFilterDto.setMonths((String[])months.keySet().toArray(new String[] {}));
		Arrays.sort(callCountFilterDto.getMonths());
		Integer[] dialCount = new Integer[callCountFilterDto.getMonths().length];
		Integer[] dialedCount = new Integer[callCountFilterDto.getMonths().length];
		for(int i = 0;i< callCountFilterDto.getMonths().length;i++) {
			if(dialMonths.get(callCountFilterDto.getMonths()[i]) != null)
				dialCount[i] = dialMonths.get(callCountFilterDto.getMonths()[i]).getValue();
			else
				dialCount[i] = 0;
			
			if(dialedMonths.get(callCountFilterDto.getMonths()[i]) != null)
				dialedCount[i] = dialedMonths.get(callCountFilterDto.getMonths()[i]).getValue();
			else
				dialedCount[i] = 0;
		}
		
		callCountFilterDto.setDialCount(dialCount);
		callCountFilterDto.setDialedCount(dialedCount);
		return callCountFilterDto;
	}
	
	public CallCountFilterDto getCallCountWithMonth(String loanId,String month) {
		//1.获取主叫次数
		CallCountFilterDto callCountFilterDto = new CallCountFilterDto();
		Map<String,String> param = new HashMap<>();
		param.put("loanId", loanId);
		param.put("type", "DIAL");
		param.put("month", month);
		List<PieChartDto>  dialCountList = intMxCallDetailsMapper.getCallCountWithMonthChart(param);
		
		//2.获取被叫次数
		param.put("type", "DIALED");
		List<PieChartDto> dialedCountList = intMxCallDetailsMapper.getCallCountWithMonthChart(param);
		
		//3.处理主叫和被叫月份缺失的情况，取两个的合集，对于没有的则补0次
		//2018-10
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		int dayCount = 30;
		try {
			dayCount = DateUtil.getDaysOfMonth(sdf.parse(month));
		} catch (ParseException e) {
			
		}
		Map<String,String> months = new HashMap<>();
		for(int i=0;i<dayCount;i++) {
			if(i+1<10)
				months.put("0"+(i+1), "0"+(i+1));
			else
				months.put(i+1+"", i+1+"");
		}
		Map<String,PieChartDto> dialMonths = new HashMap<>();
		Map<String,PieChartDto> dialedMonths = new HashMap<>();
		if(CollectionUtils.isNotEmpty(dialCountList)) {
			for(PieChartDto pieChartDto : dialCountList) {
				if(StringUtils.isBlank(pieChartDto.getName()))
					continue;
				
				dialMonths.put(pieChartDto.getName(), pieChartDto);
			}
		}
		if(CollectionUtils.isNotEmpty(dialedCountList)) {
			for(PieChartDto pieChartDto : dialedCountList) {
				if(StringUtils.isBlank(pieChartDto.getName()))
					continue;
				
				dialedMonths.put(pieChartDto.getName(), pieChartDto);
			}
		}
		
		callCountFilterDto.setMonths((String[])months.keySet().toArray(new String[] {}));
		Arrays.sort(callCountFilterDto.getMonths());
		Integer[] dialCount = new Integer[callCountFilterDto.getMonths().length];
		Integer[] dialedCount = new Integer[callCountFilterDto.getMonths().length];
		for(int i = 0;i< callCountFilterDto.getMonths().length;i++) {
			if(dialMonths.get(callCountFilterDto.getMonths()[i]) != null)
				dialCount[i] = dialMonths.get(callCountFilterDto.getMonths()[i]).getValue();
			else
				dialCount[i] = 0;
			
			if(dialedMonths.get(callCountFilterDto.getMonths()[i]) != null)
				dialedCount[i] = dialedMonths.get(callCountFilterDto.getMonths()[i]).getValue();
			else
				dialedCount[i] = 0;
		}
		
		callCountFilterDto.setDialCount(dialCount);
		callCountFilterDto.setDialedCount(dialedCount);
		return callCountFilterDto;
	}

	@Override
	public CallCountFilterDto getStrangerNumberStatistics(String loanId, String userId) {
		//1.获取主叫次数
		CallCountFilterDto callCountFilterDto = new CallCountFilterDto();
		Map<String,String> param = new HashMap<>();
		param.put("loanId", loanId);
		param.put("type", "DIAL");
		param.put("userId", userId);
		List<PieChartDto>  dialCountList = intMxCallDetailsMapper.getStrangerNumberStatistics(param);
		
		//2.获取被叫次数
		param.put("type", "DIALED");
		List<PieChartDto> dialedCountList = intMxCallDetailsMapper.getStrangerNumberStatistics(param);
		
		//3.处理主叫和被叫月份缺失的情况，取两个的合集，对于没有的则补0次
		Map<String,String> months = new HashMap<>();
		Map<String,PieChartDto> dialMonths = new HashMap<>();
		Map<String,PieChartDto> dialedMonths = new HashMap<>();
		if(CollectionUtils.isNotEmpty(dialCountList)) {
			for(PieChartDto pieChartDto : dialCountList) {
				if(StringUtils.isBlank(pieChartDto.getName()))
					continue;
				
				months.put(pieChartDto.getName(), pieChartDto.getName());
				dialMonths.put(pieChartDto.getName(), pieChartDto);
			}
		}
		if(CollectionUtils.isNotEmpty(dialedCountList)) {
			for(PieChartDto pieChartDto : dialedCountList) {
				if(StringUtils.isBlank(pieChartDto.getName()))
					continue;
				
				months.put(pieChartDto.getName(), pieChartDto.getName());
				dialedMonths.put(pieChartDto.getName(), pieChartDto);
			}
		}
		
		callCountFilterDto.setMonths((String[])months.keySet().toArray(new String[] {}));
		Arrays.sort(callCountFilterDto.getMonths());
		Integer[] dialCount = new Integer[callCountFilterDto.getMonths().length];
		Integer[] dialedCount = new Integer[callCountFilterDto.getMonths().length];
		for(int i = 0;i< callCountFilterDto.getMonths().length;i++) {
			if(dialMonths.get(callCountFilterDto.getMonths()[i]) != null)
				dialCount[i] = dialMonths.get(callCountFilterDto.getMonths()[i]).getValue();
			else
				dialCount[i] = 0;
			
			if(dialedMonths.get(callCountFilterDto.getMonths()[i]) != null)
				dialedCount[i] = dialedMonths.get(callCountFilterDto.getMonths()[i]).getValue();
			else
				dialedCount[i] = 0;
		}
		
		callCountFilterDto.setDialCount(dialCount);
		callCountFilterDto.setDialedCount(dialedCount);
		return callCountFilterDto;
	}

	@Override
	public CallCountFilterDto getCallPeriodStatistics(String loanId) {
		CallCountFilterDto callCountFilterDto = new CallCountFilterDto();
		Map<String,String> param = new HashMap<>();
		param.put("loanId", loanId);
		param.put("type", "DIAL");
		List<PieChartDto>  dialCountList = intMxCallDetailsMapper.getCallPeriodStatistics(param);
		
		//2.获取被叫次数
		param.put("type", "DIALED");
		List<PieChartDto> dialedCountList = intMxCallDetailsMapper.getCallPeriodStatistics(param);
		
		//3.处理主叫和被叫月份缺失的情况，取两个的合集，对于没有的则补0次
		Map<String,String> months = new HashMap<>();
		for(int i=0;i<24;i++) {
			if(i<10)
				months.put("0"+i, "0"+i);
			else
				months.put(i+"", i+"");
		}
		
		Map<String,PieChartDto> dialMonths = new HashMap<>();
		Map<String,PieChartDto> dialedMonths = new HashMap<>();
		if(CollectionUtils.isNotEmpty(dialCountList)) {
			for(PieChartDto pieChartDto : dialCountList) {
				if(StringUtils.isBlank(pieChartDto.getName()))
					continue;
				
				dialMonths.put(pieChartDto.getName(), pieChartDto);
			}
		}
		if(CollectionUtils.isNotEmpty(dialedCountList)) {
			for(PieChartDto pieChartDto : dialedCountList) {
				if(StringUtils.isBlank(pieChartDto.getName()))
					continue;
				
				dialedMonths.put(pieChartDto.getName(), pieChartDto);
			}
		}
		
		callCountFilterDto.setMonths((String[])months.keySet().toArray(new String[] {}));
		Arrays.sort(callCountFilterDto.getMonths());
		Integer[] dialCount = new Integer[callCountFilterDto.getMonths().length];
		Integer[] dialedCount = new Integer[callCountFilterDto.getMonths().length];
		for(int i = 0;i< callCountFilterDto.getMonths().length;i++) {
			if(dialMonths.get(callCountFilterDto.getMonths()[i]) != null)
				dialCount[i] = dialMonths.get(callCountFilterDto.getMonths()[i]).getValue();
			else
				dialCount[i] = 0;
			
			if(dialedMonths.get(callCountFilterDto.getMonths()[i]) != null)
				dialedCount[i] = dialedMonths.get(callCountFilterDto.getMonths()[i]).getValue();
			else
				dialedCount[i] = 0;
		}
		Calendar cale = Calendar.getInstance();  
        
        String[] monthArry=new String[6];
        for(int i=0;i<6;i++) {
        	
        }
		callCountFilterDto.setDialCount(dialCount);
		callCountFilterDto.setDialedCount(dialedCount);
		return callCountFilterDto;
	}

	@Override
	public CallCountFilterDto getCallPeriodStatisticsWithMonth(String loanId, String month) {
		//1.获取主叫次数
		CallCountFilterDto callCountFilterDto = new CallCountFilterDto();
		Map<String,String> param = new HashMap<>();
		param.put("loanId", loanId);
		param.put("type", "DIAL");
		param.put("month", month);
		List<PieChartDto>  dialCountList = intMxCallDetailsMapper.getCallPeriodStatisticsWithMonth(param);
		
		//2.获取被叫次数
		param.put("type", "DIALED");
		List<PieChartDto> dialedCountList = intMxCallDetailsMapper.getCallPeriodStatisticsWithMonth(param);
		
		//3.处理主叫和被叫月份缺失的情况，取两个的合集，对于没有的则补0次
		//2018-10
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		int dayCount = 30;
		try {
			dayCount = DateUtil.getDaysOfMonth(sdf.parse(month));
		} catch (ParseException e) {
			
		}
		Map<String,String> months = new HashMap<>();
		for(int i=0;i<24;i++) {
			if(i<10)
				months.put("0"+i, "0"+i);
			else
				months.put(i+"", i+"");
		}
		Map<String,PieChartDto> dialMonths = new HashMap<>();
		Map<String,PieChartDto> dialedMonths = new HashMap<>();
		if(CollectionUtils.isNotEmpty(dialCountList)) {
			for(PieChartDto pieChartDto : dialCountList) {
				if(StringUtils.isBlank(pieChartDto.getName()))
					continue;
				
				dialMonths.put(pieChartDto.getName(), pieChartDto);
			}
		}
		if(CollectionUtils.isNotEmpty(dialedCountList)) {
			for(PieChartDto pieChartDto : dialedCountList) {
				if(StringUtils.isBlank(pieChartDto.getName()))
					continue;
				
				dialedMonths.put(pieChartDto.getName(), pieChartDto);
			}
		}
		
		callCountFilterDto.setMonths((String[])months.keySet().toArray(new String[] {}));
		Arrays.sort(callCountFilterDto.getMonths());
		Integer[] dialCount = new Integer[callCountFilterDto.getMonths().length];
		Integer[] dialedCount = new Integer[callCountFilterDto.getMonths().length];
		for(int i = 0;i< callCountFilterDto.getMonths().length;i++) {
			if(dialMonths.get(callCountFilterDto.getMonths()[i]) != null)
				dialCount[i] = dialMonths.get(callCountFilterDto.getMonths()[i]).getValue();
			else
				dialCount[i] = 0;
			
			if(dialedMonths.get(callCountFilterDto.getMonths()[i]) != null)
				dialedCount[i] = dialedMonths.get(callCountFilterDto.getMonths()[i]).getValue();
			else
				dialedCount[i] = 0;
		}
		
		callCountFilterDto.setDialCount(dialCount);
		callCountFilterDto.setDialedCount(dialedCount);
		return callCountFilterDto;
	}

	@Override
	public CreditReportMessageDto queryCallCountList(Map<String, Object> map) { 
		CreditReportMessageDto dto = new CreditReportMessageDto();
		dto.setData(intMxCallDetailsMapper.queryCallCountList(map));
		dto.setTotalSize(intMxCallDetailsMapper.queryCallCountListCount(map));
		return dto;
	}

	@Override
	public CreditReportMessageDto queryCallCountListWithMonth(Map<String, Object> map) {
		CreditReportMessageDto dto = new CreditReportMessageDto();
		dto.setData(intMxCallDetailsMapper.queryCallCountListWithMonth(map));
		dto.setTotalSize(intMxCallDetailsMapper.queryCallCountListWithMonthCount(map));
		return dto;
	}

	@Override
	public CreditReportMessageDto getCallDetailList(Map<String, Object> map) {
		CreditReportMessageDto dto = new CreditReportMessageDto();
		dto.setData(intMxCallDetailsMapper.getCallDetailList(map));
		dto.setTotalSize(intMxCallDetailsMapper.getCallDetailListCount(map));
		return dto;
	}

	@Override
	public CreditReportMessageDto getAddressBookList(Map<String, Object> map) {
		CreditReportMessageDto dto = new CreditReportMessageDto();
		dto.setData(intMxCallDetailsMapper.getAddressBookList(map));
		dto.setTotalSize(intMxCallDetailsMapper.getAddressBookListCount(map));
		return dto;
	}

	@Override
	public MxAlipayUserInfoDto getMxAlipayUserInfo(String loanId) {
		//1.先查出最新一条的支付宝个人信息,
		MxAlipayUserInfoDto  userInfo = intMxCallDetailsMapper.getMxAlipayUserInfo(loanId);
		if(userInfo != null){
			MxAlipayUserInfoDto  WealthInfo = intMxCallDetailsMapper.getMxAlipayWealth(userInfo.getReqNo());
			userInfo.setYeb(WealthInfo.getYeb());
			userInfo.setYue(WealthInfo.getYue());
			userInfo.setHuabaiLimit(WealthInfo.getHuabaiLimit());
			userInfo.setHuabaiBalance(WealthInfo.getHuabaiBalance());
			userInfo.setFund(WealthInfo.getFund());
			
			if(userInfo != null && StringUtils.isNotBlank(userInfo.getMessage())) {
				JSONObject json = JSON.parseObject(userInfo.getMessage());
				if(json != null && json.get("message") != null) {
					userInfo.setMessage(json.get("message").toString());
				}
			}
			//如果message为空，则主动去查
			if(StrUtil.isEmpty(userInfo.getMessage())){
				MXUserReportCall mxUserReportCall = freshMoXie.getReportMessage(loanId, MoXieTaskType.ALIPAY);
				if(null != mxUserReportCall && mxUserReportCall.getResult()){
					userInfo.setMessage(mxUserReportCall.getMessage());
				}
			}
		}
		return userInfo;
	}

	@Override
	public List<MxAlipayBankDto> getMxAlipayBankList(String loanId) {
		MxAlipayUserInfoDto  userInfo = intMxCallDetailsMapper.getMxAlipayUserInfo(loanId);
		
		return intMxCallDetailsMapper.getMxAlipayBankList(userInfo.getReqNo());
	}

	@Override
	public List<MxAlipayAddressDto> getMxAlipayAddressList(String loanId) {
		MxAlipayUserInfoDto  userInfo = intMxCallDetailsMapper.getMxAlipayUserInfo(loanId);
		return intMxCallDetailsMapper.getMxAlipayAddressList(userInfo.getReqNo());
	}

	@Override
	public CreditReportMessageDto getZXApplyInfo(Map<String, Object> map) {
		CreditReportMessageDto dto = new CreditReportMessageDto();
		dto.setData(intMxCallDetailsMapper.getZXApplyInfo(map));
		dto.setTotalSize(intMxCallDetailsMapper.getZXApplyInfoCount((String)map.get("reqNo")));
		return dto;	
		}

	@Override
	public CreditReportMessageDto getZXOrderInfo(Map<String, Object> map) {
		CreditReportMessageDto dto = new CreditReportMessageDto();
		dto.setData(intMxCallDetailsMapper.getZXOrderInfo(map));
		dto.setTotalSize(intMxCallDetailsMapper.getZXOrderInfoCount((String)map.get("reqNo")));
		return dto;	
	}

	@Override
	public CreditReportMessageDto getZXOrderDetailInfo(Map<String, Object> map) {
		CreditReportMessageDto dto = new CreditReportMessageDto();
		dto.setData(intMxCallDetailsMapper.getZXOrderDetailInfo(map));
		dto.setTotalSize(intMxCallDetailsMapper.getZXOrderDetailInfoCount((String)map.get("reqNo")));
		return dto;	
	}

	@Override
	public CreditReportPortrait getZXBasicInfo(String cardNo) {
		CreditReportPortrait dto  = intMxCallDetailsMapper.getZXBasicInfo(cardNo);
		if(dto != null) {
			dto.setLocation(intMxCallDetailsMapper.getZXAddr(dto.getReqNo()));
			dto.setApplyPlatformCount(Integer.parseInt(intMxCallDetailsMapper.getZXApplyInfoCount(dto.getReqNo())+""));
		}
		return dto;
	}

	@Override
	public MxOperatorReportDto getMxCheckBlackInfo(String loanId) {
		//获取到message
		IntMxUserBasicInfo IntMxUserBasicInfo  = intMxUserBasicInfoMapper.selectByLoanId(loanId);
		MxOperatorReportDto dto = intMxCallDetailsMapper.getMxCheckBlackInfo(loanId);
		if(IntMxUserBasicInfo != null && StringUtils.isNotBlank(IntMxUserBasicInfo.getMessage())) {
			JSONObject json = JSON.parseObject(IntMxUserBasicInfo.getMessage());
			if(json != null && json.get("message") != null) {
				dto.setMessage(json.get("message").toString());
			}
		}
		
		//如果message为空，则主动去查
		if(StrUtil.isEmpty(dto.getMessage())){
			MXUserReportCall mxUserReportCall = freshMoXie.getReportMessage(loanId, MoXieTaskType.CARRIER);
			if(null != mxUserReportCall && mxUserReportCall.getResult()){
				dto.setMessage(mxUserReportCall.getMessage());
			}
		}
		return dto;
	}

	@Override
	public MXCheckSearchInfo getMxUserSearchInfo(String loanId) {
		MXCheckSearchInfo searchInfo = intMxCallDetailsMapper.getMxUserSearchInfo(loanId);
		if (searchInfo != null && StringUtils.isNotEmpty(searchInfo.getSearchedOrgType())) {
			String searchedOrgType = searchInfo.getSearchedOrgType();
			searchInfo.setSearchedOrgType(transOrgType(searchedOrgType));
		}
		if (searchInfo != null && StringUtils.isNotEmpty(searchInfo.getRegisterOrgType())) {
			String registerOrgType = searchInfo.getRegisterOrgType();
			searchInfo.setRegisterOrgType(transOrgType(registerOrgType));
		}
		return searchInfo;
	}
	

	/**
	 * 转换企业类型
	 * @param originalType
	 * @return
	 */
	private String transOrgType(String originalType) {
		List<String> sList = Arrays.asList(originalType.split(","));
		StringBuilder stringBuilder = new StringBuilder();
		for (String orgType : sList) {
			if (orgType.equalsIgnoreCase("ZHENGXIN")) {
				stringBuilder.append("征信机构").append(",");
			} else if (orgType.equalsIgnoreCase("DATA_PLATFORM")) {
				stringBuilder.append("数据平台").append(",");
			} else if (orgType.equalsIgnoreCase("BANK")) {
				stringBuilder.append("银行").append(",");
			} else if (orgType.equalsIgnoreCase("CUSTOMER_FINANCE")) {
				stringBuilder.append("消费金融").append(",");
			} else if (orgType.equalsIgnoreCase("CASH_LOAN")) {
				stringBuilder.append("现金贷").append(",");
			} else if (orgType.equalsIgnoreCase("P2P")) {
				stringBuilder.append("P2P理财").append(",");
			} else if (orgType.equalsIgnoreCase("CREDITPAY")) {
				stringBuilder.append("信用支付").append(",");
			} else if (orgType.equalsIgnoreCase("CONSUMSTAGE")) {
				stringBuilder.append("消费分期").append(",");
			} else if (orgType.equalsIgnoreCase("COMPENSATION")) {
				stringBuilder.append("信用卡代偿").append(",");
			} else if (orgType.equalsIgnoreCase("DIVERSION")) {
				stringBuilder.append("导流平台").append(",");
			} else if (orgType.equalsIgnoreCase("DATACOVERGE")) {
				stringBuilder.append("数据聚合平台").append(",");
			} else {
				stringBuilder.append(orgType).append(",");
			}
		}
		return stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1);
	}
	
	
	
	@Override
	public BRDeviceInfo getDeviceInfo(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return intMxCallDetailsMapper.getDeviceInfo(map);
	}

	@Override
	public CreditReportMessageDto getDeviceDetailInfo(Map<String, Object> map) {
		CreditReportMessageDto dto = new CreditReportMessageDto();
		dto.setData(intMxCallDetailsMapper.getDeviceDetailInfo(map));
		dto.setTotalSize(intMxCallDetailsMapper.getDeviceDetailInfoCount(map));
		return dto;	
	}

	@Override
	public void save(EdcDeviceDto deviceDto) {
		MarvelsLogUtil.getInstance().info("获取设备信息:"+JSON.toJSONString(deviceDto));
		String apps = "";
		
		Map<String,String> map = new HashMap<>();
		ThirdProperties  prop = ThirdPropertiesInitUtil.getThirdProperties();
		if(prop != null && StringUtils.isNotBlank(prop.getApp_name_evil())) {
			String [] evilAppArry = prop.getApp_name_evil().split(",");
			if(evilAppArry != null) {
				for(String appName : evilAppArry) {
					map.put(appName, appName);
				}
			}
		}
		
		String isRunEvilApp = "否";
		if(deviceDto.getRunApps() != null) {
			for(String app:deviceDto.getRunApps()) {
				apps = apps + app+",";
				if(map.get(app) != null)
					isRunEvilApp = "是";
			}
			if(apps.contains(","))
				apps = apps.substring(0, apps.lastIndexOf(","));
			// 防止字段太长而导致入库失败
			apps = apps.length() > 500 ? apps.substring(0, 500) : apps;
			deviceDto.setApps(apps);
		}
		
		int appCt=0;
		int eyiAppCt=0;
		if(deviceDto.getInstallApps()!= null) {
			for(String app:deviceDto.getInstallApps()) {
				if(app.contains("贷"))
					appCt++;
				
				if(map.get(app) != null)
					eyiAppCt ++;
			}
			
			deviceDto.setAppCt(appCt);
		}
		
		deviceDto.setRun_spite_app(isRunEvilApp);
		deviceDto.setSpite_app_cn(eyiAppCt);
		deviceDto.setCreatedBy("system");
		deviceDto.setUpdatedBy("system");
		deviceDto.setCreatedDate(new Date());
		deviceDto.setUpdatedDate(new Date());
		edcDeviceInfoMapper.insert(deviceDto);
	}

	@Override
	public EdcDeviceDto selectByLoanId(String loanId) {
		// TODO Auto-generated method stub
		EdcDeviceDto dto = edcDeviceInfoMapper.selectByLoanId(loanId);
		if(dto != null) {
			if("CHN-UNICOM".equals(dto.getOperator()))
				dto.setOperator("中国联通");
			else if("CHINA_MOBILE".equals(dto.getOperator()))
				dto.setOperator("中国移动");
			else if("CHINA_TELECOM".equals(dto.getOperator()))
				dto.setOperator("中国电信");
		}
		
		return dto;
	}

	@Override
	public YDUserInfoDto selectYDUserInfoDto(String loanId) {
		loanId = intMxCallDetailsMapper.selectYDReqNo(loanId);
		YDUserInfoDto  YDUserInfoDto  = intMxCallDetailsMapper.getYDUserInfoDto(loanId);
		List<YDFeatureDto> featureList = intMxCallDetailsMapper.getYDFeatureDto(loanId);
		YDUserInfoDto.setFeatureList(featureList);
		if(CollectionUtils.isNotEmpty(featureList)) {
			for(YDFeatureDto featureDto : featureList) {
				featureDto.setUser_feature_type(ConvertUtil.convertFeature(featureDto.getUser_feature_type()));
			}
		}
		return YDUserInfoDto;
	}

	@Override
	public YDLendBehaviorDto selectYDLendBehaviorDto(String loanId) {
		loanId = intMxCallDetailsMapper.selectYDReqNo(loanId);
		YDLendBehaviorDto  ydLendBehaviorDto  = intMxCallDetailsMapper.selectYDLendBehaviorDto(loanId);
		ydLendBehaviorDto.setIndustryLendBehaviorList(intMxCallDetailsMapper.selectYDIndustryLendBehaviorDto(loanId));
		return ydLendBehaviorDto;
	}

	@Override
	public YDDeviceLinkDto selectYDDeviceLinkDto(String loanId) {
		loanId = intMxCallDetailsMapper.selectYDReqNo(loanId);
		YDDeviceLinkDto  linkDto = intMxCallDetailsMapper.selectYDDeviceLinkDto(loanId);
		YDDeviceLinkDto  linkDto1 = intMxCallDetailsMapper.selectYDlinkUserDetail(loanId);
		if(linkDto == null)
			linkDto = new YDDeviceLinkDto();
		if(linkDto1 != null) {
			linkDto.setPartner_mark_count(linkDto1.getPartner_mark_count());
			linkDto.setCourt_dishonest_count(linkDto1.getCourt_dishonest_count());
			linkDto.setOnline_dishonest_count(linkDto1.getOnline_dishonest_count());
			linkDto.setLiving_attack_count(linkDto1.getLiving_attack_count());
		}
		
		linkDto.setDeviceList(intMxCallDetailsMapper.selectYDDeviceInfoDto(loanId));
		linkDto.setFraud_device_count(intMxCallDetailsMapper.selectFraudDeviceCount(loanId)+"");
		linkDto.setDevice_living_attack_count(intMxCallDetailsMapper.selectLivingAttackCount(loanId)+"");
		linkDto.setLink_fraud_device_count(intMxCallDetailsMapper.selectOtherFraudDeviceCount(loanId)+"");
		linkDto.setLink_living_attack_count(intMxCallDetailsMapper.selectOtherLivingAttackCount(loanId)+"");
		return linkDto;
	}

	@Override
	public EdcZxPersonalNewCredit getZXNewCredit(String loanId) {
		return intMxCallDetailsMapper.getZXNewCredit(loanId);
	}
	
	private static boolean isEmpty(Object str){
		if(str == null || StrUtil.isEmpty(str.toString())){
			return true;
		}
		return false;
	}
	
}
